package org.jeecg.modules.workorder.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.workorder.entity.WorkingOrder;
import org.jeecg.modules.workorder.vo.OpenBomVO;
import org.jeecg.modules.workorder.vo.OpenWorkingOrderVO;
import org.jeecg.modules.workorder.vo.WorkingOrderPageVO;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @Description: 生产工单
 * @Date:   2021-09-09
 * @Version: V1.0
 */
public interface WorkingOrderMapper extends BaseMapper<WorkingOrder> {

	/** 编辑节点状态*/
	void updateTreeNodeStatus(@Param("id") String id,@Param("status") String status);

	/**通过订单id查询拟生成的开单单列表*/
	List<OpenWorkingOrderVO> queryOpenListByOrderId(@Param("orderId") String orderId,@Param("styleId") String styleId);

	/**通过订单id查询金的物料分析列表*/
	List<OpenBomVO> queryGoldBomListByOrderId(@Param("orderId") String orderId,@Param("styleId") String styleId);
	
	/**通过订单id查询配石的物料分析列表*/
	List<OpenBomVO> queryStoneBomListByOrderId(@Param("orderId") String orderId,@Param("styleId") String styleId);
	
	/**通过订单id查询配件的物料分析列表*/
	List<OpenBomVO> queryAccessoryBomListByOrderId(@Param("orderId") String orderId,@Param("styleId") String styleId);

	/**查询某个父工单ID下的最大拆分页*/
	@Select("select max(pn) from working_order where pid = #{pid}")
	String getMaxPnByPID(@Param("pid") String pid);
	
	/**查询某个订单ID下的最大拆分页*/
	@Select("select max(pn) from working_order where order_info_id = #{orderInfoId}")
	String getMaxPnByOrderInfoId(@Param("orderInfoId") String orderInfoId);

	/**查询生产工单数据列表 无分页*/
	List<WorkingOrder> selectWorkingOrderList(@Param(Constants.WRAPPER) Wrapper<WorkingOrder> queryWrapper);

	/**根据生产工单ID查询详情*/
	WorkingOrderPageVO selectWorkingOrderPageVOById(@Param("id") String id);
	
	
	/**根据一批工单ID查询根据工单号分组的统计数*/
	@Select("<script>"
			+ " SELECT * FROM working_order WHERE id IN "
			+ " <foreach item='item' index='index' collection='idsList' open='(' separator=',' close=')'>"
			+ " #{item}"
            + " </foreach>"
            + " GROUP BY work_num,store_by"
            + " </script>")
	List<WorkingOrder> countGroupWorkNumByIds(@Param("idsList") List<String> idsList);
	
	/**查询生产工单数据列表 分页*/
	IPage<WorkingOrder> selectWorkingOrderPage(Page<WorkingOrder> page, @Param(Constants.WRAPPER) Wrapper<WorkingOrder> queryWrapper);

	/**
	 * 通过工单号查询
	 * @param workNum
	 * @return
	 */
	@Select("select a.work_num As workNum, a.pn, a.style_code_num as styleCodeNum,"
			+ " b.code_num as orderCodeNum, c.gold_karat_id as karat, d.gold_content as goldContent"
			+ " from working_order a left join order_info b on a.order_info_id = b.id "
			+ " left join style_customer_info c on a.style_cus_info_id = c.id "
			+ " left join gold_karat d on c.gold_karat_id = d.id "
			+ " where a.work_num = #{workNum}")
	Map<String,String> queryByWorknum(@Param("workNum") String workNum);
}
